package servlet.community;

import dao.UserDao;
import domain.Community;
import org.junit.Test;
import service.community.CommunityService;
import service.community.CommunityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddCommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        String newCommunityName = req.getParameter("newCommunityName");
        String newCommunityInfo = req.getParameter("newCommunityInfo");
        CommunityService communityService = new CommunityServiceImpl();
        Community community = new Community();
        community.setCommunity_name(newCommunityName);
        community.setCommunity_info(newCommunityInfo);
        if (newCommunityName == null || newCommunityInfo == null){
            System.out.println("添加失败，有个添加的输入框为空！");
            req.getRequestDispatcher("/admin/error/error404.jsp").forward(req,resp);
        } else{
            communityService.addCommunity(community);
            System.out.println("添加社区成功！");
            resp.sendRedirect("CommunityInfo");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
